/*
   * author: op
   * createTime : 2019/8/9
*/
import axios from 'axios'

//  接口统一前缀

let baseUrl = 'http://localhost:8080';

//  超时时间
axios.defaults.timeout = 20000;
// axios.defaults.baseUrl = '/apis'


//  设置默认请求头
axios.defaults.headers = {
  'Accept':'application/json, text/plain, */*',
  'X-Requested-With': 'XMLHttpRequest',
  'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
};

axios.interceptors.request.use(
  config => {
    if (localStorage.getItem('token')) {
      config.headers['XX-Token'] = localStorage.getItem('token');
      config.headers['XX-Device-Type'] = 'wxapp';
    }
    return config;
  }
);
//  http response 拦截器(拦截响应数据以及状态码等)
axios.interceptors.response.use(
  //200+、300+状态码
  response => {
    return response;
  },
  //  400+、500+状态码
  error => {
    if(error.response){
      if(error.response.status !== 404){}
    }
    else{}
    return error.response
  });

export default {

  post (url, data) {
    setData(data);
    return axios({
      method: 'post',
      url: baseUrl + url,
      data: data,
    }).catch((error) => {
      console.log(error.message)
    })
  },
  get (url, params) {
    setData(params);
    return axios({
      method: 'get',
      url: baseUrl+url,
      params: params,
    }).catch((error) => {
      console.log(error.message)
    })
  },
  delete (url, data) {
    setData(data);
    return axios({
      method: 'delete',
      url: baseUrl + url,
      data: data,
    }).catch((error) => {
      console.log(error.message)
    })
  },
  put (url, data) {
    setData(data);
    return axios({
      method: 'put',
      url: baseUrl + url,
      data: data,
    }).catch((error) => {
      console.log(error.message)
    })
  },
}

//  在发送数据给后台前统一处理
function setData(data){
  return data;
}
